package main

import (
	"context"
	"fmt"
	"time"
)

func watch1(ctx1 context.Context) {
	for {
		select {
		case <-ctx1.Done():
			fmt.Println("父context 退出：", time.Now().Unix())
			return
		default:
			fmt.Println("父context 监控中：", time.Now().Unix())
			time.Sleep(time.Second)
		}
	}
}
func watch2(ctx2 context.Context) {
	ctx3, _ := context.WithCancel(ctx2)
	go watch3(ctx3)
	for {
		select {
		case <-ctx2.Done():
			fmt.Println("ctx2 退出:", time.Now().Unix())
			return
		default:
			fmt.Println("ctx2 监控中。。。:", time.Now().Unix())
			time.Sleep(time.Second)
		}
	}
}
func watch3(ctx3 context.Context) {
	for {
		select {
		case <-ctx3.Done():
			fmt.Println("ctx3退出：", time.Now().Unix())
			return
		default:
			fmt.Println("ctx3监控中...", time.Now().Unix())
			time.Sleep(time.Second)
		}
	}
}
func main() {
	ctx1, cancel := context.WithCancel(context.Background())
	go watch1(ctx1)
	ctx2, _ := context.WithCancel(ctx1)
	go watch2(ctx2)
	fmt.Println("sleep:")
	time.Sleep(time.Second * 3)
	cancel()
	time.Sleep(time.Second)
	fmt.Println("main....")
}
